<?php
class NewsModelDao {

	function NewsModelDao() {
	}

	function findAllNews($active, $date_beg, $date_end) {
		$news = array();
		$query = "SELECT N.ID_NEWS, N.ID_EVENT, N.TITLE, N.INTRO, N.BODY, N.DATE_CREAT, N.DATE_BEG, N.DATE_END,"
					." N.ACTIVE FROM TBL_NEWS N WHERE N.ACTIVE = $active AND N.DATE_BEG <= $date_beg AND N.DATE_END >= $date_end"
					." ORDER BY N.DATE_CREAT DESC";
		$result = mysql_query ($query) or trigger_error(mysql_error(), E_USER_ERROR);
		while ($row = mysql_fetch_assoc($result)) {
			$news[] = new NewsModel($row);
		}
		return $news;
	}

	function findAllNewsForAdmin() {
		$news = array();
		$query = "SELECT N.ID_NEWS, N.ID_EVENT, N.TITLE, N.INTRO, N.BODY, N.DATE_CREAT, N.DATE_BEG, N.DATE_END,"
					." N.ACTIVE FROM TBL_NEWS N ORDER BY N.DATE_CREAT DESC";
		$result = mysql_query ($query) or trigger_error(mysql_error(), E_USER_ERROR);
		while ($row = mysql_fetch_assoc($result)) {
			$news[] = new NewsModel($row);
		}
		return $news;
	}

	function findArchive() {
		$news = array();
		$query = "SELECT N.ID_NEWS, N.ID_EVENT, N.TITLE, N.INTRO, N.BODY, N.DATE_CREAT, N.DATE_BEG, N.DATE_END,"
			." N.ACTIVE FROM TBL_NEWS N WHERE N.ACTIVE = 1 AND N.DATE_END < SYSDATE() ORDER BY DATE_CREAT";
		$result = mysql_query ($query) or trigger_error(mysql_error(), E_USER_ERROR);
		while ($row = mysql_fetch_assoc($result)) {
			$news[] = new NewsModel($row);
		}
		return $news;
	}

	function insertNews($news) {
		$query  = "INSERT INTO TBL_NEWS(TITLE, INTRO, BODY, DATE_CREAT, DATE_BEG, DATE_END, "
					."ACTIVE) VALUES('".$news->getTitle()."', '"
					.$news->getIntro()."', '".$news->getBody()."', '".$news->getDate_creat()."', '"
					.$news->getDate_beg()."', '".$news->getDate_end()."', '".$news->getActive()."')";
		$result = mysql_query($query) or trigger_error(mysql_error(), E_USER_ERROR);
	}

	function updateNews($news) {
		$query  = "UPDATE TBL_NEWS SET TITLE = '".$news->getTitle()."', INTRO = '".$news->getIntro()."', BODY = '".$news->getBody()."',"
				."DATE_CREAT = '".$news->getDate_creat()."', DATE_BEG = '".$news->getDate_beg()."', DATE_END = '".$news->getDate_end()."',"
				."ACTIVE = '".$news->getActive()."' WHERE ID_NEWS=".$news->getId_news();
		$result = mysql_query($query) or trigger_error(mysql_error(), E_USER_ERROR);
	}

	function deleteNews($id) {
		$query = "DELETE FROM TBL_NEWS WHERE ID_NEWS=$id";
       	$result = mysql_query($query) or trigger_error(mysql_error(), E_USER_ERROR);
		return $result;
	}	
	
	function selectNews($id) {
		$query = "SELECT N.ID_NEWS, N.ID_EVENT, N.TITLE, N.INTRO, N.BODY, N.DATE_CREAT, N.DATE_BEG, N.DATE_END,"
					." N.ACTIVE FROM TBL_NEWS N WHERE N.ID_NEWS = $id";
		$result = mysql_query ($query) or trigger_error(mysql_error(), E_USER_ERROR);
		$row = mysql_fetch_assoc($result);
		$news = new NewsModel($row);
		return $news;
	}
	
}
?>